package com.martin.kill;

import java.util.Timer;
import java.util.TimerTask;

import com.martin.R;

import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.PixelFormat;
import android.graphics.Point;
import android.graphics.PorterDuff;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.view.LayoutInflater;
import android.view.SurfaceView;
import android.view.View;
 
import android.widget.ImageView;
import android.widget.TextView;

public class CimovFligeSurface {

	private Context tContext;
	private SurfaceView tSurfaceView;
	private View tItemView;

	private Timer tTimer = null;
	TimerTask tFligeTimerTask = null;

	private Bitmap tFligeBitmap;
	private int tFligeTick = 0;
	private final int tFligeTickMax = 20;

	public CimovFligeSurface(Context tInputContext,
			SurfaceView tInputSurfaceView) {
		tContext = tInputContext;
		tSurfaceView = tInputSurfaceView;
		tSurfaceView.setZOrderOnTop(true); 
//		tSurfaceView.setEGLConfigChooser(8, 8, 8, 8, 16, 0);
		tSurfaceView.getHolder().setFormat(PixelFormat.TRANSLUCENT);
		tItemView = LayoutInflater.from(tContext).inflate(R.layout.list_item,
				null);
		tItemView.setEnabled(false);
		tItemView.setVisibility(View.INVISIBLE);
	}

	public void Exit() {
		if (tTimer != null)
			tTimer.cancel();
		if (tFligeTimerTask != null)
			tFligeTimerTask.cancel();
	}

	public View RetView() {
		return tItemView;
	}

	@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
	@SuppressLint("NewApi")
	public void Start(int tInputStartX, int tInputStartY, int tInputEndX,
			int tInputEndY, Drawable img1, Drawable img2,String appName) {
		TextView tTempTextView1;
		ImageView image, imgleft;
		int tTempIntX, tTempIntY;

		tTempIntX = (tInputEndX * 3 + tInputEndX) / 4;
		tTempIntY = (tInputStartY - (tInputEndY - tInputStartY) / 2);

		tTempTextView1 = (TextView) tItemView.findViewById(R.id.text);
		tTempTextView1.setText(appName);
		// tTempTextView2 = (TextView) tItemView
		// .findViewById(R.id.itemselected_singer);
		image = (ImageView) tItemView.findViewById(R.id.image);
		image.setBackgroundDrawable(img1);
//		imgleft = (ImageView) tItemView.findViewById(R.id.imgleft);
//		image.setBackground(img1);
		// if (tInputSelected != 0)
		// tTempImageView.setBackground(tContext.getResources().getDrawable(
		// R.drawable.songbg_selected));
		// tTempImageView.setImageResource(R.drawable.songbg_selected);
		;
		// else
		// tTempImageView.setBackground(tContext.getResources().getDrawable(
		// R.drawable.songbg_default));

		// tTempImageView.setImageResource(R.drawable.songbg_default);
		// tTempTextView1.setText(tInputSongName);
		// tTempTextView2.setText(tInputSingerName);

		tItemView.setDrawingCacheEnabled(true);
		tItemView.buildDrawingCache();

		tFligeBitmap = Bitmap.createBitmap(tItemView.getDrawingCache());

		tFligeTick = 0;
		if (tTimer != null)
			tTimer.cancel();
		if (tFligeTimerTask != null)
			tFligeTimerTask.cancel();

		p1.x = tInputStartX;
		p1.y = tInputStartY;
		p2.x = tTempIntX;
		p2.y = tTempIntY;
		p3.x = tInputEndX;
		p3.y = tInputEndY;

		tTimer = new Timer();
		tFligeTimerTask = new TimerTask() {
			@Override
			public void run() {
				if (tFligeTick >= tFligeTickMax) {
					// System.out.println("Before exit");
					// tTimer.purge();
					// System.out.println("After exit");
					tTimer.cancel();
					tFligeTimerTask.cancel();
					Canvas tTempCanvas = tSurfaceView.getHolder().lockCanvas();
					tTempCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
					tSurfaceView.getHolder().unlockCanvasAndPost(tTempCanvas);
				} else {
					tFligeTick += 1;

					Matrix tTempMatrix = new Matrix();
					float tTempRate = ((float) (tFligeTickMax - tFligeTick))
							/ tFligeTickMax + 0.2f;

					tTempMatrix.postScale(tTempRate, tTempRate);
					Bitmap tTempNewBitmap = Bitmap.createBitmap(tFligeBitmap,
							0, 0, tFligeBitmap.getWidth(),
							tFligeBitmap.getHeight(), tTempMatrix, true);

					Point tTempPoint = calculate(tFligeTick);

					Canvas tTempCanvas = tSurfaceView.getHolder().lockCanvas();

					tTempCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
					tTempCanvas.drawBitmap(tTempNewBitmap, tTempPoint.x,
							tTempPoint.y, null);
					tSurfaceView.getHolder().unlockCanvasAndPost(tTempCanvas);
				}
			}
		};

		tTimer.schedule(tFligeTimerTask, 0, 10);
	}

	Point p1 = new Point(0, 0);
	Point p2 = new Point(0, 0);
	Point p3 = new Point(0, 0);

	private Point calculate(int tInputParam) {
		Point tTempRet = new Point();
		float tTempPos = (float) tInputParam / tFligeTickMax;

		tTempRet.x = (int) (p1.x * (1.0f - tTempPos) * (1.0f - tTempPos)
				* (1.0f - tTempPos) + p2.x * 3.0f * tTempPos
				* (1.0f - tTempPos) * (1.0f - tTempPos) + p3.x * 3.0f
				* tTempPos * tTempPos * (1.0f - tTempPos) + p3.x * tTempPos
				* tTempPos * tTempPos);

		tTempRet.y = (int) (p1.y * (1.0f - tTempPos) * (1.0f - tTempPos)
				* (1.0f - tTempPos) + p2.y * 3.0f * tTempPos
				* (1.0f - tTempPos) * (1.0f - tTempPos) + p3.y * 3.0f
				* tTempPos * tTempPos * (1.0f - tTempPos) + p3.y * tTempPos
				* tTempPos * tTempPos);

		return tTempRet;
	}

}
